package com.baor.service.impl;

import com.baor.dao.ClazzDao;
import com.baor.dao.StudentDao;
import com.baor.dao.TeacherDao;
import com.baor.domain.ClassVo;
import com.baor.domain.StuVo;
import com.baor.service.ClazzService;
import com.baor.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ClazzService")
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzDao clazzDao;

    @Override
    public AjaxResult<?> clazzList(Map<String,Object> params) {
        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex",(pageIndex - 1)*pageSize);
        List<ClassVo> classList = clazzDao.clazzList(params);
        List<Integer> pageCount = clazzDao.pageCount(params);
        Map<String,Object> res = new HashMap<>();
        res.put("classList",classList);
        res.put("pageCount",pageCount);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> deleteClazz(int id) {
        int row = clazzDao.deleteClazz(id);
        if(row == 0){
            return AjaxResult.error();
        }else{
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult<?> updateClazz(ClassVo classVo) {
        int row = clazzDao.updateById(classVo);
        if(row == 0){
            return AjaxResult.error();
        }else{
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult<?> addClazz(ClassVo classVo) {
        int row = clazzDao.insert(classVo);
        if(row == 0){
            return AjaxResult.error();
        }else{
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult<?> teacherList() {
        return AjaxResult.success(this.clazzDao.teacherList());
    }

    @Override
    public AjaxResult<?> clazzStu(Map<String, Object> params) {
        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex",(pageIndex - 1) * pageSize);
        List<Map<String,Object>> stuList = clazzDao.clazzStu(params);
        List<Integer> pageCount =  clazzDao.stuPageCount(params);
        Map<String,Object> res = new HashMap<>();
        res.put("pageCount",pageCount);
        res.put("stuList",stuList);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> stuDetail(int stuId) {
        return AjaxResult.success(this.clazzDao.stuDetail(stuId));
    }

    @Override
    public AjaxResult<?> deleteStu(int stuId) {
        int row = clazzDao.deleteStu(stuId);
        if(row == 0){
            return AjaxResult.error();
        }else{
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult<?> updateBatchById(Map<String,Object> params) {
        List<Integer> stuId = (List<Integer>) params.get("stuId");
        int row = 0;
        for(int i = 0;i<stuId.size();i++){
            params.put("stuId",stuId.get(i));
            row = clazzDao.updateBatchById(params);
        }
        if(row != 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }

    }

    @Override
    public AjaxResult<?> classSelect() {
        return AjaxResult.success(this.clazzDao.classSelect());
    }

    @Override
    public AjaxResult<?> addSubject(Map<String, Object> params) {
        List<Integer> subjectId = (List<Integer>) params.get("subjectId");
        System.out.println(subjectId);
        int row = 0;
        for(int i = 0;i<subjectId.size();i++){
            params.put("subjectId",subjectId.get(i));
            row = clazzDao.addSubject(params);
        }
        if(row != 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> selectSub(Map<String,Object> params) {
        return AjaxResult.success(clazzDao.selectSub(params));
    }

}
